<?php
class Pageley_Serializer_HttpTests extends Pageley_Test_Case {
    private $type;
    private $serializer;
    
    protected function setUp() {
        $string = new Pageley_Domain_Attribute_Basic("string");
        $number = new Pageley_Domain_Attribute_Basic("number", null, new Pageley_Serializer_Converter_Number());
        $timestamp = new Pageley_Domain_Attribute_Timestamp("date");
        $phoneNumber = new Pageley_Domain_Attribute_Basic("phoneNumber");
        $active = new Pageley_Domain_Attribute_Boolean("active");
        $this->type = new Pageley_Domain_BasicType(
            "Pageley_Commerce",
            "Article",
            new Pageley_Util_ArrayMap(array(
                "name" => $string,
                "price" => $number,
                "date" => $timestamp,
                "phoneNumber" => $phoneNumber,
                "active" => $active
            ))
        );
        $this->serializer = Pageley_Serializer_Format::$HTTP->getSerializer();
    }
    
    public function testDefaultDeserialize() {
        $values = new Pageley_Util_ArrayMap(array(
            "name" => "Llama Spray",
            "price" => "2.20",
            "date" => "2008-06-01 12:43:16",
            "phoneNumber" => "+3231234567",
            "active" => "true"
        ));
        $entity = $this->serializer->deserialize($values, $this->type);
        $this->assertNotNull($entity);
        $this->assertEquals($this->type, $entity->getType());
        $this->assertEquals("Llama Spray", $entity->get("name"));
        $this->assertEquals(2.20, $entity->get("price"));
        $this->assertEquals(Pageley_Util_Timestamp::parse("2008-06-01 12:43:16"), $entity->get("date"));
        $this->assertEquals("+3231234567", $entity->get("phoneNumber"));
        $this->assertTrue($entity->get("active"));
    }
    
    public function testDeserializeWithFormat() {
        $values = new Pageley_Util_ArrayMap(array(
            "name" => "Llama Spray",
            "price" => "2,20",
            "price.format" => "0,00",
            "date" => "01/06/2008 12:43:16",
            "date.format" => "d/m/Y H:i:s",
            "phoneNumber" => "+3231234567",
            "active" => "false"
        ));
        $entity = $this->serializer->deserialize($values, $this->type);
        $this->assertNotNull($entity);
        $this->assertEquals($this->type, $entity->getType());
        $this->assertEquals("Llama Spray", $entity->get("name"));
        $this->assertEquals(2.20, $entity->get("price"));
        $this->assertEquals(Pageley_Util_Timestamp::parse("2008-06-01 12:43:16"), $entity->get("date"));
        $this->assertEquals("+3231234567", $entity->get("phoneNumber"));
        $this->assertFalse($entity->get("active"));
    }
}